
#include <stdint.h>

#include <encoding_manager.h>
#include <fonts_manager.h>


int Utf16BE_GetUnicodeFromBuffer(unsigned char *bufstart, unsigned char *bufend, unsigned int *unicode) 
{
    if (bufstart + 2 > bufend) 
    {
        return 0;  // 缓冲区不足，无法读取两个字节
    }

    // 对于be是正常顺序保存，高字节放在高地址，低字节放在低地址
    uint16_t first_word = (bufstart[0] << 8) | bufstart[1];
    *unicode = first_word;
    return 2;  // 返回2表示utf16be用2byte表示一个字符
}


// 返回1表示支持Utf16BE编码，返回0表示不支持
static int SupportUtf16BE(unsigned char *bufhead)
{
    const unsigned char utf16be_bom[] = {0xFE, 0xFF};
    return (bufhead[0] == utf16be_bom[0] && 
            bufhead[1] == utf16be_bom[1]);
}





static EncodingOpr Utf16BE_EncodingOpr =
{
    .name = "utf16be",
    .iHeadLen = 2, 
    .GetUnicodeFromBuffer = Utf16BE_GetUnicodeFromBuffer,
    .Support = SupportUtf16BE,
};



void RegisterUtf16BE(void)
{
    RegisterEncodingOpr(&Utf16BE_EncodingOpr);
}



// 为Utf16BE_EncodingOpr设置支持Utf16BE编码格式的字体
int SetSupportFontForUtf16BE(void)
{   
    AddFontOprToEncodingOpr(GetFontOprByName("ft"), &Utf16BE_EncodingOpr);
    AddFontOprToEncodingOpr(GetFontOprByName("ascii"), &Utf16BE_EncodingOpr);
    AddFontOprToEncodingOpr(GetFontOprByName("hzk"), &Utf16BE_EncodingOpr);
    return 0;
}

